package com.xywy.bloodandroid.act;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.User;
import com.xywy.bloodandroid.util.Constants;
import com.xywy.bloodandroid.util.MD5;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;
import com.xywy.bloodandroid.vollery.PostRequest;
import com.xywy.bloodandroid.vollery.VolleyManager;

/**
 * 修改姓名Activity
 */
public class ChangeNameActivity extends BaseActivity {

	private TextView middle_title; // title
	private RelativeLayout left_img; // 返回按钮
	private EditText et_change_name; // 改名字的et

	private TextView right_title; // 改名字的et
	private ImageView iv_clear; // 清理
	String name;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.activity_change_name);
		getDataPreviousAct();
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText("名字");
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		right_title = (TextView) findViewById(R.id.right_title);
		right_title.setText("保存");
		right_title.setVisibility(View.VISIBLE);
		et_change_name = (EditText) findViewById(R.id.et_change_name);
		et_change_name.setText(User.getInstance(context).getAccountstr());
		iv_clear = (ImageView) findViewById(R.id.iv_clear);
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
		right_title.setOnClickListener(this);
		iv_clear.setOnClickListener(this);

	}

	private void getDataPreviousAct() {
		Intent intent = getIntent();
		name = intent.getStringExtra("user_name");
	}

	@Override
	protected void initData() {
		// et_change_name.setText(User.getInstance(this).getName());
		et_change_name.setText(name);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			ChangeNameActivity.this.finish();
			break;

		case R.id.right_title:

			if (et_change_name.getText().toString().trim().length() > 0
					&& null != et_change_name.getText().toString().trim()) {

				// TODO 应该上传到服务器上吧 ？？
			} else {
				showToast("姓名不能为空！");
			}
			changOnServer();

			break;
		case R.id.iv_clear:
			et_change_name.setText("");
			break;
		}
	}

	/*
	 * 修改姓名
	 */
	private void changOnServer() {
		String sign = null;
		try {
			sign = MD5.md5s(Constants.Sign + Constants.tag);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		String url = "http://api.wws.xywy.com/index.php?act=kbb&fun=resetProperty&tag="
				+ Constants.tag + "&sign=" + sign;
		PostRequest<String> postRequest = new PostRequest<String>(url,
				String.class, new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						showToast("修改失败！");
					}

					@Override
					public void onSuccess(String response) {
						try {
							JSONObject jsonObject = new JSONObject(response);
							int state = jsonObject.getInt("state");
							if (state == 200) {
								User.getInstance(context).setName(
										et_change_name.getText().toString()
												.trim());
								showToast("姓名更改成功！");
								finish();
							} else {
								showToast("修改失败！");
							}
						} catch (JSONException e) {
							showToast("修改失败！");
							e.printStackTrace();
						}
					}
				});

		Map<String, String> params = new HashMap<String, String>();
		// params.put("sign", sign);
		// params.put("tag", Constants.tag);
		params.put("userid", User.getInstance(context).getUserid());
		params.put("app_id", "2");
		params.put("accountstr", et_change_name.getText().toString().trim());
		int sex = 1;
		if (User.getInstance(this).getSex().equals("0")) { // 存在本地 0 男， 1女；
			sex = 1;
		} else { // 存在服务器 1男，2女
			sex = 2;
		}
		params.put("sex", sex + "");
		params.put("height", User.getInstance(this).getHeight());
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String time = User.getInstance(this).getBirthday();
		Date date = null;
		try {
			date = format.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		params.put("birthday", date.getTime() / 1000 + "");
		params.put("keyword", "edit");

		System.out.println("sign:" + sign);
		System.out.println("tag:" + Constants.tag);
		System.out.println("userid:" + User.getInstance(context).getUserid());
		System.out.println("app_id:" + 2);
		System.out.println("accountstr:"
				+ et_change_name.getText().toString().trim());
		System.out.println("sex:" + sex);
		System.out.println("height:" + User.getInstance(this).getHeight());
		System.out.println("birthday:" + date.getTime() / 1000 + "");
		System.out.println("keyword:" + "edit");

		postRequest.setParams(params);
		postRequest.setIsParseJson(false);
		VolleyManager.addRequest(postRequest, "");

	}
}
